package com.greentreecs.fms.controller;

import com.greentreecs.fms.constant.ExceptionConstant;
import com.greentreecs.fms.pojo.dto.IncDetailRecDTO;
import com.greentreecs.fms.pojo.param.CreateIncDetailParam;
import com.greentreecs.fms.pojo.param.QueryIncDetailParam;
import com.greentreecs.fms.pojo.param.UpdateIncDetailParam;
import com.greentreecs.fms.pojo.response.GeneralResponse;
import com.greentreecs.fms.service.IncomeAnalyseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.SQLException;
import java.util.List;

/**
 * @author 刀氏春秋
 * @date 2025/2/18
 */
@RequestMapping("/income-analyse")
@RestController
@Api(value = "收入明细统计与分析", tags = "收入明细统计与分析")
public class IncomeDetailController {
    private final static Logger logger = LoggerFactory.getLogger(IncomeDetailController.class);
    @Autowired
    private IncomeAnalyseService incomeAnalyseService;

    @ApiOperation(value = "创建新收入明细", notes = "创建新收入明细")
    @PostMapping(value = "/create-new-income-detail")
    public GeneralResponse<String> createNewIncomeDetail(@RequestBody CreateIncDetailParam param) {
        GeneralResponse<String> response = new GeneralResponse<>();
        // 获取收入明细数据插入结果
        try {
            incomeAnalyseService.createNewIncomeDetail(param);
            response.setCode(ExceptionConstant.SUCCESS);
            response.setMessage(ExceptionConstant.SUCCESS_MESSAGE);
        } catch (Exception e) {
            response.fillResponseByException(e);
            logger.error("收入明细创建失败:{}", e.getMessage());
        }
        return response;
    }

    @ApiOperation(value = "查询收入明细", notes = "查询收入明细")
    @PostMapping(value = "/query-income-detail")
    public GeneralResponse<List<IncDetailRecDTO>> queryIncomeDetail(@RequestBody QueryIncDetailParam param) {
        GeneralResponse<List<IncDetailRecDTO>> response = new GeneralResponse<>();
        try {
            response.fillResponseWith(incomeAnalyseService.queryIncomeDetail(param));
        } catch (SQLException e) {
            response.fillResponseByException(e);
            logger.error("收入明细查询失败:{}", e.getMessage());
        }
        return response;
    }

    @ApiOperation(value = "删除收入明细", notes = "删除收入明细")
    @PostMapping(value = "/delete-income-detail")
    private GeneralResponse<Integer> deleteIncomeDetail(@RequestBody List<Integer> params) {
        GeneralResponse<Integer> response = new GeneralResponse<>();
        try {
            response.fillResponseWith(incomeAnalyseService.deleteIncomeDetail(params));
        } catch (SQLException e) {
            response.fillResponseByException(e);
            logger.error("删除明细失败:{}", e.getMessage());
        }
        return response;
    }

    @ApiOperation(value = "更新收入明细", notes = "更新收入明细")
    @PostMapping(value = "/update-income-detail")
    private GeneralResponse<Integer> updateIncomeDetail(@RequestBody UpdateIncDetailParam param) {
        GeneralResponse<Integer> response = new GeneralResponse<>();
        try {
            response.fillResponseWith(incomeAnalyseService.updateIncomeDetail(param));
        } catch (SQLException e) {
            response.fillResponseByException(e);
            logger.error("更新明细失败:{}", e.getMessage());
        }
        return response;
    }
}
